package imper.graphic.view;

import imper.engine.entity.Country;
import imper.engine.entity.Industry;
import imper.engine.entity.IndustryType;
import imper.engine.entity.Resources;
import imper.engine.util.ParameterManager;
import imper.graphic.ImageManager;
import imper.graphic.listener.AddIndustryListener;
import imper.graphic.listener.RemoveIndustryListener;
import imper.graphic.listener.UpgradeIndustryListener;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import TWLSlick.RootPane;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.ProgressBar;

public class IndustryView extends BarMenuView
{

	private List<IndustryGroup> _groupList = null;

	@Override
	public void init(GameContainer pGameContainer, StateBasedGame pStateBasedGame) throws SlickException
	{
		super.init(pGameContainer, pStateBasedGame);
		//
		_groupList = new ArrayList<IndustryGroup>();
		//
		int nb = 0;
		for (IndustryType industry : ParameterManager.getInstance().getIndustryType().values())
		{
			int posX = 40 + ((nb % 3) * 250);
			int posY = 50 + ((nb / 3) * 120);
			_groupList.add(new IndustryGroup(industry, this, posX, posY));
			nb++;
		}

	}

	@Override
	public void render(GameContainer pGameContainer, StateBasedGame pStateBasedGame, Graphics pGraphics) throws SlickException
	{
		// pGraphics.drawString("Industrie", 100, 100);
		//
		for (IndustryGroup industryGroup : _groupList)
		{
			industryGroup.render(pGameContainer, pStateBasedGame, pGraphics);
		}

	}

	@Override
	public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException
	{
		// TODO Auto-generated method stub

	}

	@Override
	protected void setViewRootPane(RootPane pRootPane)
	{
		for (IndustryGroup industryGroup : _groupList)
		{
			industryGroup.setViewRootPane(pRootPane);
		}
	}

	@Override
	protected void setViewLayout()
	{
		for (IndustryGroup industryGroup : _groupList)
		{
			industryGroup.setViewLayout();
		}
	}

	@Override
	public int getID()
	{
		return EnumView.INDUSTRYVIEW.getId();
	}

	public void refreshView()
	{
		for (IndustryGroup industryGroup : _groupList)
		{
			industryGroup.refreshView();
		}
	}

	public class IndustryGroup
	{
		private Button _buttonMore = null;

		private Button _buttonLess = null;

		private Button _buttonUpdate = null;

		private ProgressBar _progressBar = null;

		private IndustryType _industry = null;

		private IndustryView _view;

		private int _posX;
		private int _posY;

		public IndustryGroup(IndustryType pIndustry, IndustryView pView, int pPosX, int pPosY)
		{
			_posX = pPosX;
			_posY = pPosY;
			_industry = pIndustry;
			_view = pView;
		}

		protected void setViewRootPane(RootPane pRootPane)
		{
			_buttonMore = new Button("+");
			_buttonMore.addCallback(new AddIndustryListener(_industry, _view));
			pRootPane.add(_buttonMore);
			//
			_buttonLess = new Button("-");
			_buttonLess.addCallback(new RemoveIndustryListener(_industry, _view));
			pRootPane.add(_buttonLess);
			//
			_buttonUpdate = new Button("Upgrade");
			_buttonUpdate.addCallback(new UpgradeIndustryListener(_industry, _view));
			pRootPane.add(_buttonUpdate);
			//
			_progressBar = new ProgressBar();

			pRootPane.add(_progressBar);
		}

		protected void setViewLayout()
		{
			//
			_buttonMore.adjustSize();
			_buttonMore.setSize(20, 20);
			_buttonMore.setPosition(_posX + 120, _posY + 30);
			//
			_buttonLess.adjustSize();
			_buttonLess.setSize(20, 20);
			_buttonLess.setPosition(_posX + 0, _posY + 30);
			//
			_buttonUpdate.adjustSize();
			_buttonUpdate.setSize(60, 20);
			_buttonUpdate.setPosition(_posX + 150, _posY + 30);
			//
			_progressBar.adjustSize();
			_progressBar.setSize(100, 20);
			_progressBar.setPosition(_posX + 20, _posY + 30);
			//
			// refreshView();

		}

		public void render(GameContainer pGameContainer, StateBasedGame pStateBasedGame, Graphics pGraphics) throws SlickException
		{
			Country country = ParameterManager.getInstance().getMainCountry();
			pGraphics.drawString(_industry.getName(), _posX, _posY);
			int nb = 0;
			for (Resources resource : _industry.getIn().getList())
			{
				Image image = ImageManager.getInstance().getImage(ImageManager.RESOURCE, resource.getIdResources());
				Resources resources = country.getResourceStock().getResources(resource.getIdResources());
				for (int i = 0; i < resource.getValue(); i++)
				{
					pGraphics.drawImage(image, _posX + nb, _posY + 60);
					nb = nb + 15;
					nb++;
				}
				pGraphics.drawString(resources.getValue() + "", _posX + nb, _posY + 80);
				nb = nb + 15;

			}
			pGraphics.drawString("=>", _posX + nb, _posY + 60);
			nb = nb + 20;
			for (Resources resource : _industry.getOut().getList())
			{
				Image image = ImageManager.getInstance().getImage(ImageManager.RESOURCE, resource.getIdResources());
				Resources resources = country.getResourceProd().getResources(resource.getIdResources());
				for (int i = 0; i < resource.getValue(); i++)
				{
					pGraphics.drawImage(image, _posX + nb, _posY + 60);
					nb = nb + 15;

				}
				if (resources != null)
				{
					pGraphics.drawString(resources.getValue() + "", _posX + nb, _posY + 80);
				}
				else
				{
					pGraphics.drawString("0", _posX + nb, _posY + 80);
				}
				nb = nb + 15;
			}
		}

		public void refreshView()
		{
			Country country = ParameterManager.getInstance().getMainCountry();
			Industry industry = country.getIndustry().get(_industry.getId());
			if (industry != null)
			{
				float pour = 0.0f;
				pour = ((float) industry.getProduct()) / ((float) industry.getSize());
				_progressBar.setValue(pour);
				//
				_buttonUpdate.setText("Upgrade");
				_buttonMore.setEnabled(true);
				_buttonLess.setEnabled(true);
				_progressBar.setEnabled(true);
			}
			else
			{
				_buttonUpdate.setText("Create");
				_buttonMore.setEnabled(false);
				_buttonLess.setEnabled(false);
				_progressBar.setEnabled(false);
			}
		}

	}

}
